package com.lagou.hint;

import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.ArrayList;
import java.util.Collection;

public class MyHintAlgorithm implements HintShardingAlgorithm<Integer> {
	/**
	 * 根据指定分片值路由（强制路由）
	 *
	 * @param availableTargetNames ds0,ds1
	 * @param shardingValue        0,1
	 * @return ds0或者ds1
	 */
	@Override
	public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<Integer> shardingValue) {
		Collection<String> result = new ArrayList<>();
		for (String availableTargetName : availableTargetNames) {
			for (Integer value : shardingValue.getValues()) {
				if (availableTargetName.endsWith(String.valueOf(value % 2))) {
					result.add(availableTargetName);
				}
			}
		}
		return result;
	}
}
